<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AdminUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{

    public function login(Request $request)
    {
        $request->validate([
            'username' => 'required',
            'password' => 'required'
        ]);
        $username = $request->input('username');
        // return Hash::make($request->input('password'));
        $user = AdminUser::query()->where('username', $username)->orWhere('mobile', $username)->first();
        $isAuth = Hash::check($request->input('password'), $user->password);

        if (!$user || !$isAuth) {
            return $this->error('用户名或密码错误');
        }
        $token = $user->createToken('admin');
        $user->token = $token->plainTextToken;
        return $this->success($user, '登录成功');
    }
    public function logout(Request $request)
    {
        $user = $request->user();
        $user->tokens()->delete();
        return $this->success([], '您已经成功退出');
    }
}
